package com.ie.sevice.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.comment.http.ResponseResult;
import com.ie.entity.GoodsTypeEntity;
import com.ie.mapper.admin.GoodsTypeMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hp
 */
@Service
public class GoodsTypeImpl extends ServiceImpl<GoodsTypeMapper , GoodsTypeEntity> implements GoodsTypeService {
    @Override
    public ResponseResult<Map<String, Object>> getGoodsType(GoodsTypeEntity goodsTypeEntity) {
        Integer currentPage = goodsTypeEntity.getCurrentPage();
        if (currentPage < 0 || currentPage == null) {
            currentPage = 1;
        }
        IPage<GoodsTypeEntity> ipage = new Page<>(currentPage, 5);
        IPage<GoodsTypeEntity> pageData = this.page(ipage);
        Map<String, Object> npapresult = new HashMap<>();
        npapresult.put("allGoodsTypes", pageData.getRecords());
        npapresult.put("totalPage", pageData.getPages());
        return ResponseResult.getSuccessResult(npapresult);
    }

    @Override
    public ResponseResult<Map<String, Object>> add(GoodsTypeEntity goodsTypeEntity) {
        /**
         * 1、添加商品类型用save(goodsTypeEntity)方法
         * 2、判断是否添加成功，返回结果，如果成功返回A001,否则返回A002
         */
        boolean res = save(goodsTypeEntity);
        if (res){
            return ResponseResult.getMessageResult(null , "A001");
        }
        return ResponseResult.getMessageResult(null , "A002");
    }

    @Override
    public ResponseResult<Map<String, Object>> update(GoodsTypeEntity goodsTypeEntity) {
       boolean res = updateById(goodsTypeEntity);
       if (res){
            return ResponseResult.getMessageResult(null , "A001");
        }
        return ResponseResult.getMessageResult(null , "A002");
    }

    @Override
    public ResponseResult<Map<String, Object>> delete(GoodsTypeEntity goodsTypeEntity) {
        /**
         * 1、在goddService中查找和goodstype关联的商品，返回一个数值res
         * 如果res>0 ，有关联，不能删除，则返回A001
         * 否则能删除
         * 删除成功A002
         * 删除失败A003
         * TODO Auto-generated method stub
         */
        long res = 0;
        if(res > 0){
            return ResponseResult.getMessageResult(null , "A001");
        }
        boolean res1 = removeById(goodsTypeEntity.getId());
        if (res1){
            return ResponseResult.getMessageResult(null , "A002");
        }
        return ResponseResult.getMessageResult(null , "A003");
    }

}
